﻿/*----------------------------------------------------------------
 * 
 * 版权所有：Copyright (C) 2012 
 * 
 * 文件名：UserController.cs
 * 文件功能描述：
 * 
 * 
 * 作 者：zhangq
 * 邮 箱：zhangqiang@bamabest.com
 * 日 期：2013-03-11 02:11:02
 * 
 * 
 * 修改标识： Author
 * 修改描述： 相关描述
 * 
 * ---------------------------------------------------------------- */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Common.Extensions.OrderBy;
using Common.Extensions.Where;
using DomainModel;
using EmitMapper;
using EmitMapper.MappingConfiguration;
using IManager;
using UI.JsonModel;
using WebPlatform.Models;

namespace WebPlatform.Controllers
{

    /// <summary>
    /// 
    /// </summary>
    public class UserController : Controller
    {


        public IManagerUser IMUser { get; set; }


        /// <summary>
        /// 
        /// </summary>
        public ActionResult Index()
        {
            return View();
        }



        [HttpPost]
        public JsonResult Users(string cname,string ischeck,int page = 1, int pageSize = 15)
        {

            //var companyId = Sessions.User.CompanyId;

            var p = PredicateExtension.True<UserInfo>();
            if (!string.IsNullOrEmpty(cname))
            {
                p = p.And(u=>u.Company.Name.Contains(cname));
            }
            if (!string.IsNullOrEmpty(ischeck))
            {
                p = p.And(u => u.IsCheck == (ischeck == "true" ? 1 : 0));
            }


            var count = IMUser.LoadCount(p);
            var list = IMUser.LoadAll(p, page, pageSize,
                new OrderByExpression<UserInfo, int>(u => u.UserID, true));

            var mapperCompany = ObjectMapperManager.DefaultInstance.GetMapper<CompanyInfo, CompanyVM>();
            var mapper = ObjectMapperManager.DefaultInstance.GetMapper<UserInfo, UserVM>(
                new DefaultMapConfig()
                .IgnoreMembers<UserInfo, UserVM>(new string[] { "Company" })
                //.ConvertUsing<CompanyInfo, CompanyVM>(v => mapperCompany.Map(v))
                .PostProcess<UserVM>((destination, state) => {
                    destination.Company = mapperCompany.Map(list.FirstOrDefault(e => e.CompanyID == destination.CompanyID).Company);
                    return destination; 
                })
                );



            var listVM = mapper.MapEnum(list).ToList();
            GridData<UserVM> gridData = new GridData<UserVM>();
            gridData.Rows = listVM;
            gridData.Total = count;
            return Json(gridData);
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult CheckUsers(string ids,int state = 0)
        {
            var b = false;
            if (!string.IsNullOrEmpty(ids))
            {
                var idList = ids.Split(',');
                var list = new List<int>();
                foreach (var s in idList)
                {
                    list.Add(Convert.ToInt32(s));
                }
                b = IMUser.CheckUsers(list, state);
                
            }
            return Json(b);
        }

    }
}
